
#include "interface.h"




/*****************************************************************************
 ** \brief	 main
 **
 ** \param [in]  none
 **
 ** \return 0
*****************************************************************************/
int main(void)
{
  SystemCoreClockUpdate();

	Hardware_Init();
	Software_Init();
	
	
	User_Ctrl_Info.Motor_Ctrl_OnOff = 0;
	User_Ctrl_Info.Speed_Ref = 0;
	User_Ctrl_Info.Speed_Ref_PU = _Q15((float)User_Ctrl_Info.Speed_Ref/MOTOR_SPEED_BASE);
	
	DelayTime_ms(300);

	User_Ctrl_Info.Motor_Ctrl_OnOff = 1;
	#if (USER_ADJUST_SPEED_MODE == DEBUG_ADJUST_SPEED && STARTUP_MODE == SENSOR_LESS_STARTUP)
	User_Ctrl_Info.Request_Speed = 400;
	User_Ctrl_Info.Speed_Ref = START_FINAL_SPEED;
	User_Ctrl_Info.Speed_Ref_PU = _Q15((float)User_Ctrl_Info.Speed_Ref/MOTOR_SPEED_BASE);
	User_Ctrl_Info.Motor_State = MC_INIT;
	#endif
	
    while(1)
    {
			if(User_Ctrl_Info.Timer_1ms == 1)
			{	
				User_Ctrl_Info.Timer_1ms = 0;
				
//			Fault_Check();
//			Fault_Recover();
			}
			
			UARTTxFreeIdleProc();
			
			if(User_Ctrl_Info.Timer_10ms >= 10)
			{
				User_Ctrl_Info.Timer_10ms = 0;
				printf("\r\n iq = %d,    FOC.Curr_Iq_Ref = %d",s_Curr_d_q.Iq,FOC.Curr_Iq_Ref);
//			printf("\r\n vBus = %dmV",mcMeas.vBus.phyValue * 1000);
//			printf("\r\n iBus = %dmA",mcMeas.iBus.phyValue * 1000);
//			printf("\r\n Request_Speed = %drpm	 Speed_Ref = %drpm	Real_Speed = %d",User_Ctrl_Info.Request_Speed,User_Ctrl_Info.Speed_Ref,User_Ctrl_Info.Real_Speed);
//			printf("\r\n Motor_State = %d, Fault_Status.Byte = 0x%d",User_Ctrl_Info.Motor_State,Fault_Info.Fault_Status.Byte);
		
				CAN_Rece_Proc();
			
//				CAN_Send_Test();
				
			}
			
			if(User_Ctrl_Info.Timer_100ms >= 100)
			{
				printf("\r\n iq = %d,    FOC.Curr_Iq_Ref = %d",s_Curr_d_q.Iq,FOC.Curr_Iq_Ref);
				User_Ctrl_Info.Timer_100ms = 0;	
				CAN_Send_Proc();
			}
			WDT_Restart();
    }
}

void HardFault_Handler(void)
{
	uint8_t cfghjklkjhgfdfg = 0;
	PWM_Out(DISABLE); 
	while(1)
	{
		cfghjklkjhgfdfg++;
		cfghjklkjhgfdfg++;
		cfghjklkjhgfdfg++;
	}
}


